import { Component, OnInit, } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { Events } from '../events'
// import { Platform, ToastController, NavController, Events } from '@ionic/angular';
// import { ThemeableBrowser, ThemeableBrowserOptions, ThemeableBrowserObject } from '@ionic-native/themeable-browser/ngx';
import { FormBuilder, FormGroup } from '@angular/forms';
import { WebSocketService } from '../socket';
@Component({
  selector: 'app-tab1',
  templateUrl: 'tab1.page.html',
  styleUrls: ['tab1.page.scss']
})
export class Tab1Page implements OnInit {
  customForm: FormGroup;

  // options: ThemeableBrowserOptions = {
  //   statusbar: {
  //     color: '#ffffffff'
  //   },
  //   toolbar: {
  //     height: 44,
  //     color: '#f0f0f0ff'
  //   },
  //   title: {
  //     color: '#003264ff',
  //     showPageTitle: true
  //   },
  //   backButton: {
  //     image: 'back',
  //     imagePressed: 'back_pressed',
  //     align: 'left',
  //     event: 'backPressed'
  //   },
  //   forwardButton: {
  //     image: 'forward',
  //     imagePressed: 'forward_pressed',
  //     align: 'left',
  //     event: 'forwardPressed'
  //   },
  //   closeButton: {
  //     image: 'close',
  //     imagePressed: 'close_pressed',
  //     align: 'left',
  //     event: 'closePressed'
  //   },
  //   customButtons: [
  //     {
  //       image: 'share',
  //       imagePressed: 'share_pressed',
  //       align: 'right',
  //       event: 'sharePressed'
  //     }
  //   ],
  //   menu: {
  //     image: 'menu',
  //     imagePressed: 'menu_pressed',
  //     title: 'Test',
  //     cancel: 'Cancel',
  //     align: 'right',
  //     items: [
  //       {
  //         event: 'helloPressed',
  //         label: 'Hello World!'
  //       },
  //       {
  //         event: 'testPressed',
  //         label: 'Test!'
  //       }
  //     ]
  //   },
  //   backButtonCanClose: true
  // }
  value = "value";
  data = 0.11
  bufferdata = 0.15
  applicationInterval;
  items = [];
  zpf;
  pfgs;

  constructor(public router: Router,
    public events: Events,
    private formBuilder: FormBuilder,
    private socketutil: WebSocketService,
    // private themeableBrowser: ThemeableBrowser
  ) {
    this.events.getMessage().subscribe((value) => {
      var key = value.key;
      var data = value.data;

      console.log('receive data --- ' + key + "-----" + data);
    })
  }


  ngOnInit(): void {
    var date = new Date();
    console.log('date --- ' + date);

    this.socketutil.createObservableSocket('ws://10.80.5.247:8086/websocket/appuser').subscribe(rep => {
      // debugger
      console.log(rep)
    }, error => {
      console.log("出错")
    });
    console.log('已连接 ws://121.40.165.18:8800')

    this.items = [{ name: '匿名用户', image: 'assets/img/aaa.jpg', pf: '3', content: '很好非常棒', date: '2020-01-01' },
    { name: '匿名用户', image: 'assets/img/aaa.jpg', pf: '2.5', content: '很好非常棒', date: '2020-01-02' },
    { name: '匿名用户', image: 'assets/img/aaa.jpg', pf: '4', content: '很好非常棒', date: '2020-01-11' }];
    this.pfgs = this.items.length;
    var pgsum = 0;
    for (let data of this.items) {
      pgsum += parseFloat(data.pf);
    }
    this.zpf = pgsum / this.pfgs;
    this.zpf = (this.zpf / 0.5).toFixed(0);
    this.zpf = this.zpf * 0.5;

    var that = this;
    this.applicationInterval = setInterval(() => {

      if (that.data >= 1) {
        // 停止定时器
        clearInterval(that.applicationInterval);
      } else {
        that.data = that.data + 0.06;
        if (that.bufferdata <= 1) {
          that.bufferdata = that.bufferdata + 0.1;
        }
      }
    }, 500);
    // this.events.subscribe('key', (result) => {
    //   console.log(result);
    // });
    this.customForm = this.formBuilder.group({
      // set default initial value
      starRating: [3]
    });

  }
  onClick(type) {
    if (type === 'send') {
      this.events.sendMessage({ key: '传参2111', data: '传值111' });
    } else {

      this.router.navigate(['/' + type], {
        queryParams: {
        }
      });
    }
  }

  openurl() {
    // const browser: ThemeableBrowserObject = this.themeableBrowser.create('http://www.baidu.com', '_blank', this.options);
  }
  logRatingChange(rating) {
    console.log("changed rating: ", rating);
    // do your stuff
  }
}
